package pers.cz.chat.client.handle;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import pers.cz.chat.constants.JefClolorEnum;
import pers.cz.chat.utils.LogUtils;
import pers.cz.netty.filter.IFilter;
import pers.cz.netty.handler.MessageHandler;
import pers.cz.netty.protocal.Message;
import pers.cz.netty.util.ContextHolder;

/**
 * @version 1.0
 * @description: 消息处理器
 * @author: ChengZhi 0336
 * @create: 2025-01-07 10:39
 **/
@ChannelHandler.Sharable
public class ChatMessageHandle extends MessageHandler {

	private IFilter<Message, Channel> iFilter;

	public void setFilter(IFilter<Message, Channel> filter) {
		this.iFilter = filter;
	}

	@Override
	protected void channelRead0(ChannelHandlerContext channelHandlerContext, Message message) throws Exception {

		if (iFilter.doFilter(message, channelHandlerContext.channel())) {
			return;
		}

		LogUtils.printMessage(JefClolorEnum.BLUE,"收到{}消息：{}", message.getAttachment("source"), message.getContent());
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		// 断开连接后清除登录状态
		ContextHolder.getRequestContext().clear();
		super.channelInactive(ctx);
	}
}
